/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * UpdatePriceWork.java
 * business
 * 2013-1-23下午5:15:02
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.rows;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.hibernate.SessionFactory;
import org.jackysoft.data.JdbcWork;
import org.jackysoft.util.ExcelUtil;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;

@Component
public class UpdatePriceWork implements JdbcWork {

	private static final Log logger = LogFactory.getLog(UpdatePriceWork.class);
	
	private SessionFactory sessionFactory;
	
	private File source;
	
	@Override
	public void execute(Connection connection) throws SQLException {
		if(source==null)return;

		try {
			Workbook wb = new HSSFWorkbook(new FileInputStream(source));
		    int sheetCount = wb.getNumberOfSheets();
		    Map<String,String> sqlMap = Maps.newHashMap();
		    
		    for(int i=0;i<sheetCount;i++){
		    	Sheet sheet = wb.getSheetAt(i);
		        Iterator<Row> rows  =  sheet.rowIterator();
		        while(rows.hasNext()){
		        	Row row = rows.next();
		        	Cell cCell = row.getCell(0);
		        	String code = ExcelUtil.cellString(cCell);
		        	Cell pCell = row.getCell(4);
		        	Double price = ExcelUtil.cellNumber(pCell);
		        	//logger.info(price);
		        	if(price!=0D){
		        		String sql = String.format("update product set price=%f where code='%s'"
	            			,price,code);
		        		sqlMap.put(code, sql);
		        		logger.info(sql);
		        	}
		        }
		    }
		    
		    Statement stat = connection.createStatement();
		    for(Map.Entry<String, String> entry:sqlMap.entrySet()){
		    	stat.addBatch(entry.getValue());
		    }
		    
		    stat.executeBatch();
		    
		} catch (FileNotFoundException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}

	}

	@Override
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public File getSource() {
		return source;
	}

	public void setSource(final File source) {
		this.source = source;
	}

}
